﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class FND_ITM_SQ : RibbonForm
    {
        public FND_ITM_SQ()
        {
            InitializeComponent();
        }

        #region Properties
        public string ConnectionString { get; set; }
        public string CustomerCode { get; set; }


        public string Code { get; set; }
        public string Description { get; set; }
        public string UOM { get; set; }
        public double UnitPrice { get; set; }
        
        List<string> _ItemCodes = new List<string>();

        public List<string> ItemCodes
        {
            get { return _ItemCodes; }
            set { _ItemCodes = value; }
        }
        #endregion

        #region Initialize Sales Qoute Form
        void InitializeView()
        {
            lvwSearched.Clear();
            lvwSearched.View = View.Details;

            lvwSearched.LabelEdit = false;
            lvwSearched.AllowColumnReorder = false;
            lvwSearched.FullRowSelect = true;

            lvwSearched.GridLines = false;
            lvwSearched.Sorting = SortOrder.Ascending;

            lvwSearched.Columns.Add("Code", 100);
            lvwSearched.Columns.Add("Description", 200);
            lvwSearched.Columns.Add("Unit Measure", 100);
            lvwSearched.Columns.Add("Unit Price", 200);

        }

        #endregion

        private void FND_ITM_SQ_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
            InitializeView();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            ItemManager oManager = new ItemManager();
            AddressBookManager oBPCard = new AddressBookManager();

            string sPriceListCode;

            oBPCard.ConnectionString = this.ConnectionString;
            oBPCard.Open();

            sPriceListCode = oBPCard.GetPriceListCode(this.CustomerCode);

            oBPCard.Close();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();


            DataTable dt = new DataTable();

            if (cboSearch.SelectedIndex == 0)
            {
                dt = oManager.BrowseSlsItemByCodeWithPrice(sPriceListCode, txtSearch.Text);
            }
            else
            {
                dt = oManager.BrowseSlsItemByNameWithPrice(sPriceListCode, txtSearch.Text);
            }


            oManager.Close();

            var items = from row in dt.AsEnumerable()
                        select new
                        {
                            ItemCode = row["ItemCode"].ToString(),
                            Dscription = row["Description"].ToString(),
                            UoM = row["UNITOFMEASURE"].ToString(),
                            UnitPrice = row["UNITPRICE"].ToString()
                        };


            

            lvwSearched.Items.Clear();

            ListViewItem lvwItem;
            
            foreach (var oItemUnits in items)
            {

                var ItemCodeExists = from sItem in ItemCodes
                                     where sItem.ToString() == oItemUnits.ItemCode
                                     select sItem;
                
                
                lvwItem = new ListViewItem(oItemUnits.ItemCode);
                lvwItem.ImageIndex = 0;
                lvwItem.SubItems.Add(oItemUnits.Dscription);
                lvwItem.SubItems.Add(oItemUnits.UoM);
                lvwItem.SubItems.Add(oItemUnits.UnitPrice);
                    
                lvwSearched.Items.Add(lvwItem);
                
                
            }

            
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in lvwSearched.Items)
            {
                if (lvi.Selected == true)
                {
                    this.Code = lvi.Text;
                    this.Description = lvi.SubItems[1].Text;
                    this.UOM = lvi.SubItems[2].Text;
                    if (lvi.SubItems[3].Text != "")
                    {
                        this.UnitPrice = Convert.ToDouble(lvi.SubItems[3].Text);
                        
                    }
                    else
                    {
                        this.UnitPrice = 0;
                    }
                    break;
                }
            }
            this.Close();
        }

        private void lvwSearched_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lvwSearched_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lvwSearched_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }
    }
}
